package com.jsh.erp.service.rebateMaterial;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.entities.RebateItem;
import com.jsh.erp.datasource.entities.RebateMaterial;
import com.jsh.erp.datasource.entities.RebateMaterialExample;
import com.jsh.erp.datasource.entities.RebateMaterialVo;
import com.jsh.erp.datasource.mappers.RebateMaterialMapper;
import com.jsh.erp.datasource.mappers.RebateMaterialMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class RebateMaterialService {
    private Logger logger = LoggerFactory.getLogger(RebateMaterialService.class);

    @Resource
    private RebateMaterialMapper rebateMaterialMapper;
    @Resource
    private SystemConfigService systemConfigService;

    @Resource
    private RebateMaterialMapperEx rebateMaterialMapperEx;

    public RebateMaterial getRebateMaterial(String id)throws Exception {
        RebateMaterial result=null;
        try{
            result=rebateMaterialMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public List<RebateMaterial> getRebateMaterial()throws Exception {
        RebateMaterialExample example = new RebateMaterialExample();
        example.createCriteria();
        List<RebateMaterial> list=null;
        try{
            list=rebateMaterialMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<RebateMaterialVo> select(String rebateId, int offset, int rows)throws Exception {
        List<RebateMaterialVo> list=null;
        try{
            list=rebateMaterialMapperEx.selectByConditionRebateMaterial(rebateId, offset, rows);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }


    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertRebateMaterial(JSONObject obj, HttpServletRequest request) throws Exception{
        RebateMaterial rebateMaterial = JSONObject.parseObject(obj.toJSONString(), RebateMaterial.class);
        rebateMaterial.setUploadFlag("0");
        int result=0;
        try{
            result=rebateMaterialMapper.insertSelective(rebateMaterial);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateRebateMaterial(JSONObject obj, HttpServletRequest request) throws Exception{
        RebateMaterial rebateMaterial = JSONObject.parseObject(obj.toJSONString(), RebateMaterial.class);
        rebateMaterial.setUploadFlag("0");
        int result=0;
        try{
            result = rebateMaterialMapper.updateByPrimaryKeySelective(rebateMaterial);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteRebateMaterial(String id, HttpServletRequest request)throws Exception {
        RebateMaterial rebateMaterial = new RebateMaterial();
        rebateMaterial.setId(id);
        rebateMaterial.setDeleteFlag("1");
        rebateMaterial.setUploadFlag("0");
        int result=0;
        try{
            result=rebateMaterialMapper.updateByPrimaryKeySelective(rebateMaterial);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteRebateMaterial(String ids, HttpServletRequest request)throws Exception {
        List<String> idList = StringUtil.strToStringList(ids);
        RebateMaterialExample example = new RebateMaterialExample();
        example.createCriteria().andIdIn(idList);
        RebateMaterial rebateMaterial = new RebateMaterial();
        rebateMaterial.setDeleteFlag("1");
        rebateMaterial.setUploadFlag("0");
        int result=0;
        try{
            result=rebateMaterialMapper.updateByExampleSelective(rebateMaterial,example);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

//    public int updateRebateMaterialByKey(String platformKey, String platformValue)throws Exception {
//        int result=0;
//        try{
//            RebateMaterial rebateMaterial = new RebateMaterial();
//            rebateMaterial.setPlatformValue(platformValue);
//            RebateMaterialExample example = new RebateMaterialExample();
//            example.createCriteria().andPlatformKeyEqualTo(platformKey);
//            result = rebateMaterialMapper.updateByExampleSelective(rebateMaterial, example);
//        }catch(Exception e){
//            JshException.writeFail(logger, e, systemConfigService.getLanCode());
//        }
//        return result;
//    }

    public RebateMaterial getRebateMaterialByKey(String rebateMaterialId)throws Exception {
        RebateMaterial rebateMaterial = new RebateMaterial();
        try{
            RebateMaterialExample example = new RebateMaterialExample();
            example.createCriteria().andRebateIdEqualTo(rebateMaterialId);
            List<RebateMaterial> list=rebateMaterialMapper.selectByExample(example);
            if(list!=null && list.size()>0){
                rebateMaterial = list.get(0);
            }
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return rebateMaterial;
    }

    public Long countRebateMaterial(String rebateMaterialId) {
        Long result = null;
        try {
            result = rebateMaterialMapperEx.countRebateMaterial(rebateMaterialId);
        } catch (Exception e) {
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }
}
